//
//  ByPersonalMyHandVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByPersonalMyHandVC.h"

/** 单元格 */
#import "ByJobCell.h"                 // 就业单元格

#import "ByJobViewModel.h"            // vm


@interface ByPersonalMyHandVC () <UITableViewDelegate, UITableViewDataSource, ByJobTahtMySendDelegate>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) ByJobViewModel *vm;
@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, assign) NSInteger page;

@end

@implementation ByPersonalMyHandVC

- (ByJobViewModel *)vm {
    if (!_vm) {
        _vm = [[ByJobViewModel alloc] init];
        _vm.jobThatMySendDelegate = self;
    }
    return _vm;
}
- (NSMutableArray *)items {
    if (!_items) {
        _items = [[NSMutableArray alloc] init];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我投过的";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBackBarButtonItem];
    
    [self createTableView];
    
    [self.table.mj_header beginRefreshing];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    ByTabBarController *tabBar = (ByTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [tabBar hideTabBar];
}

#pragma mark - Net request
/** 岗位查询 */
- (void)jobThatMySend {
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.uid = BY_USER.uid;
    params.page_num = [NSString stringWithFormat:@"%li", _page];
    params.page_size = @"10";
    [self.vm jobThatMySend:params];
}
- (void)jobThatMySendSuccess:(ByNetBaseResult *)result {
    
    ByNetJobModel *model = result.data;
//    ARRAR_LOG(model.results);
    /** 数据数组赋值，并刷新table，取消hud */
    
    if (_page == 1) {
        [self.items removeAllObjects];
        [self.items addObjectsFromArray:model.results];
        [self.table.mj_footer resetNoMoreData];
    } else {
        [self.items addObjectsFromArray:model.results];
    }
    
    [self.table reloadData];
    if (self.items.count == model.total.integerValue) {
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshingWithNoMoreData];
        return;
    }
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
}
- (void)jobThatMySendFailure:(ByNetBaseResult *)result {
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
    [ByCoreToolAlert alertError:result.msg];
}

#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page ++;
        [self jobThatMySend];
    }];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self jobThatMySend];
    }];
    self.table.mj_footer = footer;
    self.table.mj_header = header;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByJobCell *cell = [ByJobCell createCellInTableview:tableView];
    cell.myHandModel = self.items[indexPath.section];
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.5;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    NSLog(@"%@ memory warning", [self class]);
    
}


@end
